<?php

class Date {

    /**
     * 防止类实例化或被复制
     *
     */
    private function __construct() {
        
    }

    private function __clone() {
        
    }

    static public function isDate($date) {
        return strtotime(date('Y-m-d', strtotime($date))) === strtotime($date);
    }

    static public function isDateTime($datetime) {
        return strtotime(date('Y-m-d H:i:s', strtotime($datetime))) === strtotime($datetime);
    }

    static public function isTime($time) {
        return self::isDateTime(date('Y-m-d') . " {$time}");
    }

    /**
     * 获取时间戳
     * 
     * @param timestamp|datetime|null $datetime :时间，默认为当前时间
     * @return int|boolean
     */
    static public function toTimestamp($datetime = null) {

        if (!$datetime) {
            return time();
        }

        if (self::isDateTime($datetime)) {
            return strtotime($datetime);
        }

        if (is_numeric($datetime)) {
            return intval($datetime);
        }

        return false;
    }

    /**
     * 得到时间对应的当天日期开始时间
     *
     * @param timestamp|datetime $datetime :时间，默认为当前时间
     * @return datetime
     */
    static public function getDate($datetime = null) {
        return date('Y-m-d', self::toTimestamp($datetime));
    }

    /**
     * 得到时间对应的当天日期结束时间
     *
     * @param timestamp|datetime $datetime :时间，默认为当前时间
     * @return datetime
     */
    static public function getDateEnd($datetime = null) {
        return date('Y-m-d', self::toTimestamp($datetime) + 24 * 60 * 60);
    }

    /**
     * 计算天数差
     *
     * @param timestamp|datetime $datetime1
     * @param timestamp|datetime $datetime2
     * @return int
     */
    static public function dayDiff($datetime1, $datetime2) {

        $datetime1 = self::getDate($datetime1);
        $datetime2 = self::getDate($datetime2);

        $time1 = strtotime($datetime1);
        $time2 = strtotime($datetime2);

        $Days = round(($time2 - $time1) / 3600 / 24);
        return $Days;
    }

    /**
     * 计算月份差
     *
     * @param timestamp|datetime $datetime1
     * @param timestamp|datetime $datetime2
     * @return int
     */
    static public function monthDiff($datetime1, $datetime2) {
        $datetime1 = self::getDate($datetime1);
        $datetime2 = self::getDate($datetime2);

        $time1 = strtotime($datetime1);
        $time2 = strtotime($datetime2);

        $y1 = date('Y', $time1);
        $y2 = date('Y', $time2);
        $m1 = date('m', $time1);
        $m2 = date('m', $time2);

        return abs($y2 - $y1) * 12 + abs($m2 - $m1);
    }

    /**
     * 计算秒数差
     *
     * @param timestamp|datetime $datetime1
     * @param timestamp|datetime $datetime2
     * @return int
     */
    static public function secondDiff($datetime1, $datetime2) {
        $time1 = self::toTimestamp($datetime1);
        $time2 = self::toTimestamp($datetime2);

        return $time2 - $time1;
    }

    /**
     * 计算日期加天数后的日期
     *
     * @param timestamp|datetime|null $datetime :时间，默认为当前时间
     * @param int $day
     * @return datetime
     */
    static public function addDay($datetime = null, $day = 1) {
        return date("Y-m-d H:i:s", strtotime("+$day day", self::toTimestamp($datetime)));
    }

    /**
     * 计算日期减天数后的日期
     *
     * @param timestamp|datetime|null $datetime :时间，默认为当前时间
     * @param int $day  :如 10
     * @return datetime
     */
    static public function decDay($datetime = null, $day = 1) {
        return date("Y-m-d H:i:s", strtotime("-$day day", self::toTimestamp($datetime)));
    }

    /**
     * 获取当前时间加上时区时差后的时间
     * 
     * @param timestamp|datetime|null $datetime :时间，默认为当前时间
     * @param int $timezone
     * @return datetime
     */
    static public function addTimezone($datetime = null, $timezone = 0) {
        return date('Y-m-d H:i:s', strtotime("$timezone hours", self::toTimestamp($datetime)));
    }

    /**
     * 获取当前时间减去时区时差后的时间
     * @param timestamp|datetime|null $datetime :时间，默认为当前时间
     * @param int $timezone
     * @return datetime
     */
    static public function decTimezone($datetime = null, $timezone = 0) {
        return date('Y-m-d H:i:s', strtotime("-$timezone hours", self::toTimestamp($datetime)));
    }

    /**
     * 比较两个时间
     * @return int 1、$datetime1 > $datetime2，-1、$datetime1 < $datetime2，0、$datetime1 == $datetime2
     */
    static public function compareTime($datetime1, $datetime2) {
        $time1 = self::toTimestamp($datetime1);
        $time2 = self::toTimestamp($datetime2);

        if ($time1 > $time2)
            return 1;
        else if ($time1 == $time2)
            return 0;
        else
            return -1;
    }

    /**
     * 获取本周的周几是几号
     * @param string 星期名：Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday，默认Monday
     * @return datetime
     */
    static public function week2date($weekname = 'Monday') {
        $w = date('w');
        $week = $w == 0 ? 'last week ' : 'this week ';
        return date('Y-m-d', strtotime($week . $weekname));
    }

}
